home *** CD-ROM | disk | FTP | other *** search
- Path: news.halcyon.com!usenet
- From: normanb@halcyon.com (Norm Bryar)
- Newsgroups: comp.lang.c++
- Subject: Re: const FAQ?
- Date: Tue, 12 Mar 1996 16:28:54 GMT
- Organization: Northwest Nexus Inc.
- Message-ID: <4i48ms$qtc@news.halcyon.com>
- References: <31433D9F.4F81@eskimo.com>
- NNTP-Posting-Host: blv-pm3-ip12.halcyon.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- Sean Forde <seano@eskimo.com> wrote:
-
- >Has anyone ever figured out what all the different ways of using the
- >keyword "const" mean? Does a chart illustrating this exist? Most
- >books lightly touch this subject.
-
- > -Sean
-
- Well,...
- Declarations are read from right to left, like yiddish or something.
-
- char * const pString; // a constant pointer to (normal) chars
- const char * pcString; // a pointer to a character constant
-
- Const-correctness is a very nice thing to write into your function
- prototypes: it promises that when someone passes in an argument,
- especially by reference or pointer, you won't have screwed with it.
- Callers needn't make an auxilliary copy or anything before calling
- you.
-
- When creating a const value, the initialization should occur when the
- variable is defined.
-
- const int seconds_per_hour = 3600; // initialized constant
- seconds_per_hour = 4500; // ERROR!
-
-
- Class methods can be declared/defined with const as a promise that
- this class method will not alter any member of the class.
-
- class X
- {
- // save writes members to the archive,
- // but promises not to change any of 'em.
- BOOL Save( CArchive & archive ) const;
-
- // The equality operator promises
- // not to change anybody, x1 or x2,
- // in the call if( x1 == x2 )
- BOOL operator==( const X & toCompare ) const;
-
- };
-
- Again, const correctness is nice and should be part of your
- development effort. Conventional wisdom says, however, it's very
- difficult and probably not worth it to retro-fit const-correctness
- into legacy code.
-
- That's largely it, I should think. Helpful?
-
- --Norm
-
-